<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/7
 * Time: 17:15
 */
require 'vendor/autoload.php';
require 'connect.php';
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
use Slim\PDO\Database;
use Slim\PDO\Statement;
use Slim\PDO\Statement\SelectStatement;
$app = new \Slim\App();

$app->get('/getLines',function(Request $request,Response $response){
    $response=$response->withAddedHeader('Access-Control-Allow-Origin','*');
    $response=$response->withAddedHeader('Content-Type','application/json');
    $database=localhost();
        $selectStatement = $database->select()
            ->from('line');
        $stmt = $selectStatement->execute();
        $data = $stmt->fetchAll();
        for($x=0;$x<count($data);$x++){
            $data[$x]['num']=$x+1;
            $selectStatement = $database->select()
                ->from('province')
                ->where('id','=',$data[$x]['province_id']);
            $stmt = $selectStatement->execute();
            $data4 = $stmt->fetch();
            $data[$x]['province_name']=$data4['name'];
            $selectStatement = $database->select()
                ->from('line_city')
                ->where('line_id','=',$data[$x]['id']);
            $stmt = $selectStatement->execute();
            $data2 = $stmt->fetchAll();
            for($j=0;$j<count($data2);$j++){
                $selectStatement = $database->select()
                    ->from('city')
                    ->where('id','=',$data2[$j]['city_id']);
                $stmt = $selectStatement->execute();
                $data3 = $stmt->fetch();
                $data2[$j]['city_name']=$data3['name'];
            }
            $data[$x]['line_citys']=$data2;
            $data[$x]['count']=count($data2);
        }
        return $response->withJson(array("result" => "0", "desc" => "success",'lines'=>$data));

});


$app->get('/getLine',function(Request $request,Response $response){
    $response=$response->withAddedHeader('Access-Control-Allow-Origin','*');
    $response=$response->withAddedHeader('Content-Type','application/json');
    $database=localhost();
    $id=$request->getParam('id');
    if($id!=null||$id!=""){
        $selectStatement = $database->select()
            ->from('line')
            ->where('id','=',$id);
        $stmt = $selectStatement->execute();
        $data = $stmt->fetch();
        $selectStatement = $database->select()
            ->from('province')
            ->where('id','=',$data['province_id']);
        $stmt = $selectStatement->execute();
        $data4 = $stmt->fetch();
        $data['province_name']=$data4['name'];
        $selectStatement = $database->select()
            ->from('line_city')
            ->where('line_id','=',$id);
        $stmt = $selectStatement->execute();
        $data2 = $stmt->fetchAll();
        for($x=0;$x<count($data2);$x++){
            $data2[$x]['num']=$x+1;
            $selectStatement = $database->select()
                ->from('city')
                ->where('id','=',$data2[$x]['city_id']);
            $stmt = $selectStatement->execute();
            $data3 = $stmt->fetch();
            $data2[$x]['city_name']=$data3['name'];
        }
        $data['line_citys']=$data2;
        $data['count']=count($data2);
        return $response->withJson(array("result" => "0", "desc" => "success",'line'=>$data));
    }else{
        return $response->withJson(array("result" => "1", "desc" => "缺少id"));
    }
});

$app->post('/addLine',function(Request $request,Response $response){
    $response=$response->withAddedHeader('Access-Control-Allow-Origin','*');
    $response=$response->withAddedHeader('Content-Type','application/json');
    $database=localhost();
    $body = $request->getBody();
    $body=json_decode($body);
    $province_id=$body->province_id;
    $lowest_price=$body->lowest_price;
    $lowest_cost=$body->lowest_cost;
    $remark=$body->remark;
    if($province_id!=null||$province_id!=""){
        if($lowest_price!=null||$lowest_price!=""){
            if($lowest_cost!=null||$lowest_cost!=""){
                $selectStatement = $database->select()
                    ->from('line')
                    ->where('province_id','=',$province_id);
                $stmt = $selectStatement->execute();
                $data3 = $stmt->fetch();
                if($data3==null){
                $selectStatement = $database->select()
                    ->from('line');
                $stmt = $selectStatement->execute();
                $data = $stmt->fetchAll();
                $num=count($data)+1;
                $a=ceil($num/4);
                $bg_image="http://files.jswsx56.cn:8000/line_bg/bg".$a.".jpg";
                $insertStatement = $database->insert(array('province_id','bg_image','lowest_price','lowest_cost','remark'))
                    ->into('line')
                    ->values(array($province_id,$bg_image,$lowest_price,$lowest_cost,$remark));
                $insertId = $insertStatement->execute(false);
                return $response->withJson(array("result" => "0", "desc" => "success"));
                }else{
                    return $response->withJson(array("result"=>"3","desc"=>"该线路已存在"));
                }
            }else{
                return $response->withJson(array("result"=>"2","desc"=>"缺少lowest_cost"));
            }
        }else{
            return $response->withJson(array("result"=>"1","desc"=>"缺少lowest_price"));
        }
    }else{
        return $response->withJson(array("result"=>"4","desc"=>"缺少province_id"));
    }
});

$app->options('/alterLine',function(Request $request,Response $response){
    $response=$response->withAddedHeader('Access-Control-Allow-Origin','*');
    $response=$response->withAddedHeader('Content-Type','application/json');
    $response=$response->withAddedHeader("Access-Control-Allow-Methods", "PUT");
    return $response;
});

$app->put('/alterLine',function(Request $request,Response $response){
    $response=$response->withAddedHeader('Access-Control-Allow-Origin','*');
    $response=$response->withAddedHeader('Content-Type','application/json');
    $database=localhost();
    $body = $request->getBody();
    $body=json_decode($body);
    $province_id=$body->province_id;
    $lowest_price=$body->lowest_price;
    $lowest_cost=$body->lowest_cost;
    $remark=$body->remark;
    $id=$body->id;
    if($id!=null||$id!=""){
    if($province_id!=null||$province_id!=""){
        if($lowest_price!=null||$lowest_price!=""){
            if($lowest_cost!=null||$lowest_cost!=""){
                $selectStatement = $database->select()
                    ->from('line')
                    ->where('province_id','=',$province_id)
                    ->where('id','!=',$id);
                $stmt = $selectStatement->execute();
                $data3 = $stmt->fetch();
                if($data3==null){
                $updateStatement = $database->update(array('province_id'=>$province_id,'lowest_price'=>$lowest_price,'lowest_cost'=>$lowest_cost,'remark'=>$remark))
                    ->table('line')
                    ->where('id','=',$id);
                $affectedRows = $updateStatement->execute();
                return $response->withJson(array("result" => "0", "desc" => "success"));
                }else{
                    return $response->withJson(array("result"=>"5","desc"=>"线路已经存在"));
                }
            }else{
                return $response->withJson(array("result"=>"4","desc"=>"缺少lowest_cost"));
            }
        }else{
            return $response->withJson(array("result"=>"3","desc"=>"缺少lowest_price"));
        }
    }else{
        return $response->withJson(array("result"=>"2","desc"=>"缺少province_id"));
    }
    }else{
        return $response->withJson(array("result"=>"1","desc"=>"缺少id"));
    }
});


$app->options('/deleteLine',function(Request $request,Response $response){
    $response=$response->withAddedHeader('Access-Control-Allow-Origin','*');
    $response=$response->withAddedHeader('Content-Type','application/json');
    $response=$response->withAddedHeader("Access-Control-Allow-Methods", "DELETE");
    return $response;
});

$app->delete('/deleteLine',function(Request $request,Response $response){
    $response=$response->withAddedHeader('Access-Control-Allow-Origin','*');
    $response=$response->withAddedHeader('Content-Type','application/json');
    $database=localhost();
    $id=$request->getParam('id');//获取请求路径后数据
    $deleteStatement = $database->delete()
        ->from('line')
        ->where('id', '=', $id);
    $affectedRows = $deleteStatement->execute();
    $deleteStatement = $database->delete()
        ->from('line_city')
        ->where('line_id', '=', $id);
    $affectedRows = $deleteStatement->execute();
    return $response->withJson(array("result" => "0", "desc" => "success"));
});


$app->options('/adjustBgImage',function(Request $request,Response $response){
    $response=$response->withAddedHeader('Access-Control-Allow-Origin','*');
    $response=$response->withAddedHeader('Content-Type','application/json');
    $response=$response->withAddedHeader("Access-Control-Allow-Methods", "PUT");
    return $response;
});

$app->put('/adjustBgImage',function(Request $request,Response $response){
    $response=$response->withAddedHeader('Access-Control-Allow-Origin','*');
    $response=$response->withAddedHeader('Content-Type','application/json');
    $database=localhost();
    $selectStatement = $database->select()
        ->from('line');
    $stmt = $selectStatement->execute();
    $data = $stmt->fetchAll();
    for($a=0;$a<count($data);$a++){
        $num=$a+1;
        $y=ceil($num/4);
        $bg_image="http://files.jswsx56.cn:8000/line_bg/bg".$y.".jpg";
        $updateStatement = $database->update(array('bg_image'=>$bg_image))
            ->table('line')
            ->where('id','=',$data[$a]['id']);
        $affectedRows = $updateStatement->execute();
    }
    return $response->withJson(array("result" => "0", "desc" => "success"));
});




$app->run();

function localhost()
{
    return connect();
}

function encode($string , $skey ) {
    $strArr = str_split(base64_encode($string));
    $strCount = count($strArr);
    foreach (str_split($skey) as $key => $value)
        $key < $strCount && $strArr[$key].=$value;
    return str_replace(array('=', '+', '/'), array('O0O0O', 'o000o', 'oo00o'), join('', $strArr));
}

//解密
function decode($string, $skey) {
    $strArr = str_split(str_replace(array('O0O0O', 'o000o', 'oo00o'), array('=', '+', '/'), $string), 2);
    $strCount = count($strArr);
    foreach (str_split($skey) as $key => $value)
        $key <= $strCount  && isset($strArr[$key]) && $strArr[$key][1] === $value && $strArr[$key] = $strArr[$key][0];
    return base64_decode(join('', $strArr));
}

?>